// Copyright  2012 Petr TALLA. [petr.talla@gmail.com]
// This file is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY;  without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
// [http://www.gnu.org/copyleft/lesser.html]
//=============================================================================

package tc.crase.db;

public class XattrLNK extends Xattr {
	//static private int maxid = 1;
	
	private int id;
	private AttrId oppositeId;
	private XattrLNK opposite;
	private Xobject  parent;
	private boolean  first;
	
	public XattrLNK(String name, Xobject parent, int id, int oppositeId, String oppositeUrl, boolean first) {
		super(name);
		this.parent = parent;
		this.opposite = null; 
		this.first = first;
		this.id = id;
		//if (maxid <= id) maxid = id + 1;
		if (parent.db.maxLinkId <= id) parent.db.maxLinkId = id + 1;
		this.oppositeId = new AttrId(oppositeId, oppositeUrl);
		
		parent.attrAdd(this);
	}
	
	static public void connect( String name1, Xobject object1, String name2, Xobject object2 ) {
		XattrLNK attr1 = new XattrLNK(name1, object1, null, true);
		XattrLNK attr2 = new XattrLNK(name2, object2, attr1, false);
		
		String url2 = "";
		if (object1.getDb() != object2.getDb()) {
			url2 = object2.getDb().fileName;
		}
		attr1.opposite = attr2;
		attr1.oppositeId = new AttrId(attr2.id, url2);
	}

	public Xobject getParent() {
		return parent;
	}
	
	public int getId() {
		return id;
	}
	
	public AttrId getOppositeId() {
		return oppositeId;
	}
	
	public String linkid() {
		String name1 = name;
		String name2 = getOpposite().name;
		
		if ( name2.compareToIgnoreCase(name1) > 0 ) return name1 + "-" + name2; 
		return name2 + "-" + name1;
	}
	
	public boolean isFirst() {
		return first;
	}
	
	public XattrLNK getOpposite() {
		if (opposite != null) return opposite;
		
		assert parent != null;
		Xdb db = parent.db;
		assert parent != null;
		
		/*opposite = db.getLnk(oppositeId);
		assert opposite != null;*/
		
		opposite = (XattrLNK)oppositeId.searchAttr(db);
		
		return opposite;
	}
	
	@Override public String getValue() { 
		return new String(getOpposite().parent.getName());
	}
	
	@Override public XattrLNK getAsLNK() {
		return this;
	}
	
	private XattrLNK(String name, Xobject parent, XattrLNK opposite, boolean first) {
		super(name);
		this.parent = parent;
		this.opposite = opposite; 
		this.first = first;
		this.id = parent.db.maxLinkId++;
		if (opposite == null) {
			oppositeId = null;
		}
		else {
			String oppositeUrl = "";
			Xdb oppositeDb = opposite.parent.getDb();
			if ( parent.getDb() != oppositeDb ) oppositeUrl = oppositeDb.fileName;
			
			oppositeId = new AttrId(opposite.id, oppositeUrl);
		}
		
		parent.attrAdd(this);
	}
}
